/**
 * 
 */
package de.midnightdance.admin.disziplin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;

import de.cbf.util.ui.editentities.interfaces.ICompositeEntities;
import de.cbf.util.ui.editentities.interfaces.IEditableEntity;
import de.midnightdance.admin.ws.entities.DisziplinDB;

/**
 * @author Stefan Alter
 *
 */
public class CompositeListDisziplinen extends Composite implements
		ICompositeEntities {
	
	private List listDisziplinen;
	
	private ListenerDoubleClick listenerDoubleClick;
	private ListenerSelection listenerSelection;
	/**
	 * @param parent
	 * @param style
	 */
	public CompositeListDisziplinen(Composite parent, int style) {
		super(parent, style);		
		setLayoutData(new GridData(GridData.FILL_BOTH));
		//
		GridLayout layout = new GridLayout(1,false);
		layout.marginHeight = 0;
		layout.marginWidth  = 0;
		setLayout(layout);
		//
		doubleClickListeners 	= new ArrayList<ActionListener>();		
		selectionListeners  	= new ArrayList<ActionListener>();
		//
		listenerDoubleClick = new ListenerDoubleClick();
		listenerSelection	= new ListenerSelection();
		//
		listDisziplinen = new List(this, SWT.SINGLE|SWT.BORDER);
		listDisziplinen.setLayoutData(new GridData(GridData.FILL_BOTH));
		listDisziplinen.setSelection(-1);
		//
		registerListeners();
	}
	private void registerListeners() {
		listDisziplinen.addSelectionListener	(listenerSelection);
		listDisziplinen.addMouseListener		(listenerDoubleClick);		
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.interfaces.ICompositeEntities#getSelectedEntities()
	 */
	@Override
	public IEditableEntity[] getSelectedEntities() {
		java.util.List<IEditableEntity> resultList = new ArrayList<IEditableEntity>();		
		int [] selection = listDisziplinen.getSelectionIndices();
		for (int i = 0; i < selection.length; i++) {
			String name = listDisziplinen.getItem(selection[i]);
			Object object = listDisziplinen.getData(name);
			if (object instanceof IEditableEntity) {
				IEditableEntity entity = (IEditableEntity) object;
				resultList.add(entity);
			}
		}
		return (IEditableEntity[])resultList.toArray(new IEditableEntity[resultList.size()]);
	}

	
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.interfaces.ICompositeEntities#setEntities(java.util.Iterator)
	 */
	@Override
	public void setEntities(Iterator<IEditableEntity> iterator) {
		int[] selection = listDisziplinen.getSelectionIndices();
		listDisziplinen.removeAll();
		while (iterator.hasNext()) {
			IEditableEntity entity = (IEditableEntity) iterator.next();
			if (entity instanceof DisziplinDB) {
				DisziplinDB disziplinDB = (DisziplinDB) entity;
				listDisziplinen.add(disziplinDB.getName());
				listDisziplinen.setData(disziplinDB.getName(),disziplinDB);
			}			
		}
		listDisziplinen.setSelection(selection);
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.interfaces.ICompositeEntities#getSelectedEntity()
	 */
	@Override
	public IEditableEntity getSelectedEntity() {
		int index = listDisziplinen.getSelectionIndex();
		String name = listDisziplinen.getItem(index);
		Object object = listDisziplinen.getData(name);
		if (object instanceof IEditableEntity) {
			IEditableEntity entity = (IEditableEntity) object;
			return entity;
		}
		return null;
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.interfaces.ICompositeEntities#setSelectedEntity(de.cbf.util.ui.editentities.interfaces.IEditableEntity)
	 */
	@Override
	public void setSelectedEntity(IEditableEntity value)
			throws IllegalArgumentException {
		if (value instanceof DisziplinDB) {
			DisziplinDB disziplinDB = (DisziplinDB) value;			
			int index = listDisziplinen.indexOf(disziplinDB.getName());
			listDisziplinen.setSelection(index);
		}
	}
	
	private java.util.List<ActionListener> doubleClickListeners;
	@Override
	public boolean deRegisterDoubleClickListener(ActionListener listener) {
		return doubleClickListeners.remove(listener);
	}
	@Override
	public boolean registerDoubleClickListener(ActionListener listener) {
		return doubleClickListeners.add(listener);	
	}
	
	private class ListenerDoubleClick extends MouseAdapter{		
		@Override
		public void mouseDoubleClick(MouseEvent e) {
			ActionEvent event = new ActionEvent(CompositeListDisziplinen.this, ActionEvent.ACTION_PERFORMED, "doubleClick");
			for (ActionListener listener : doubleClickListeners) {
				listener.actionPerformed(event);
			}
		}		
	}
	private java.util.List<ActionListener> selectionListeners;
	@Override
	public boolean deRegisterSelectionListener(ActionListener listener) {
		return selectionListeners.remove(listener);
	}
	@Override
	public boolean registerSelectionListener(ActionListener listener) {
		return selectionListeners.add(listener);
	}
	private class ListenerSelection extends SelectionAdapter{		
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event = new ActionEvent(CompositeListDisziplinen.this, ActionEvent.ACTION_PERFORMED, "selection");
			for (ActionListener listener : selectionListeners) {
				listener.actionPerformed(event);
			}
		}		
	}

	

	

}
